﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Event.NCL;
using CPPEI.Coolzon.Service.Contract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.UI.Views.EventProxies
{
    public class RespChangeControlStateEventHandler : BaseResponseHandler<RespChangeControlStateEventData>
    {
        protected override void OnHandle(RemoteEventProxy remoteEventProxy, ServiceProxy.MainServiceRef.Response response, RespChangeControlStateEventData responseData)
        {
            if (responseData.UserIdentity == UIContext.Current.UserIdentity)
            {
                if (responseData.ControlState == ControlState.OutOfControl)
                {
                    NCLChangeControlStateEvent @event = new NCLChangeControlStateEvent();
                    @event.ControlState = responseData.ControlState;
                    @event.State = NCLEventState.Finished;
                    EventBus.Instance.PublishAsync(@event);

                    UIContext.Current.EventProcessMode = EventProcessMode.OutOfControl;

                    // 设置在控制模式下的标识为false
                    UIContext.Current.IsUnderControl = false;
                }
                else if (responseData.ControlState == ControlState.UnderControl)
                {
                    NCLChangeControlStateEvent @event = new NCLChangeControlStateEvent();
                    @event.ControlState = responseData.ControlState;
                    @event.State = NCLEventState.Finished;
                    EventBus.Instance.PublishAsync(@event);

                    UIContext.Current.EventProcessMode = EventProcessMode.UnderControl;

                    // 发送接受控制的请求到服务器,把本地的视图状态清空，避免展示端将自己的状态发送到服务器上
                    remoteEventProxy.RenderView.ResetViewStateNotUpdatePart();

                    // 设置在控制模式下的标识为true
                    UIContext.Current.IsUnderControl = true;
                }
            }
        }
    }
}
